package cn.edu.neosoft.sunbo509.finefood.Activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.edu.neosoft.sunbo509.finefood.Fragment.CollectFragment;
import cn.edu.neosoft.sunbo509.finefood.Fragment.IndexFragment;
import cn.edu.neosoft.sunbo509.finefood.Fragment.SearchFragment;
import cn.edu.neosoft.sunbo509.finefood.Fragment.UserFragment;
import cn.edu.neosoft.sunbo509.finefood.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
        IndexFragment.OnFragmentInteractionListener,
        CollectFragment.OnFragmentInteractionListener,
        SearchFragment.OnFragmentInteractionListener,
        UserFragment.OnFragmentInteractionListener{

    LinearLayout ll_index,ll_collect,ll_search,ll_user;
    Fragment fragment_index,fragment_collect,fragment_user,fragment_search;
    ImageView iv_index,iv_collect,iv_search,iv_user;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    private String userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=getIntent();//接受从登陆页面传来的userid
        userid= intent.getStringExtra("userid");
        InitView();
        InitEvent();
        SetSelect(0);
    }

    private  void InitView(){
        ll_collect=(LinearLayout)findViewById(R.id.ll_collect);
        ll_index=(LinearLayout)findViewById(R.id.ll_index);
        ll_search=(LinearLayout)findViewById(R.id.ll_search);
        ll_user =(LinearLayout)findViewById(R.id.ll_user);
        iv_collect=(ImageView)findViewById(R.id.iv_collect);
        iv_index=(ImageView)findViewById(R.id.iv_index);
        iv_search=(ImageView)findViewById(R.id.iv_search);
        iv_user=(ImageView)findViewById(R.id.iv_user);
    }
    private void InitEvent(){
        ll_index.setOnClickListener(this);
        ll_collect.setOnClickListener(this);
        ll_search.setOnClickListener(this);
        ll_user.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        ResetImage();
        switch (v.getId()) {
            case R.id.ll_index:
                SetSelect(0);
                break;
            case R.id.ll_collect:
                SetSelect(1);
                break;
            case R.id.ll_search:
                SetSelect(2);
                break;
            case R.id.ll_user:
                SetSelect(3);
                break;
            default:
                break;
        }
    }

    private void ResetImage(){
        iv_index.setImageResource(R.drawable.index0);
        iv_collect.setImageResource(R.drawable.collect0);
        iv_search.setImageResource(R.drawable.search0);
        iv_user.setImageResource(R.drawable.user0);
    }

    private void SetSelect(int i){
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        HideFragement();
        switch (i){
            case 0:
                if(fragment_index==null){
                    fragment_index=new IndexFragment();
                    //向fragment_index传递userid
                    Bundle bundle=new Bundle();
                    bundle.putString("userid",userid);
                    fragment_index.setArguments(bundle);
                    fragmentTransaction.add(R.id.f1_content,fragment_index);
                }
                else{
                    fragmentTransaction.show(fragment_index);
                }
                iv_index.setImageResource(R.drawable.index1);
                break;
            case 1:
                if (fragment_collect==null){
                    fragment_collect=new CollectFragment();
                    //向fragment_collect传递userid
                    Bundle bundle=new Bundle();
                    bundle.putString("userid",userid);
                    fragment_collect.setArguments(bundle);
                    fragmentTransaction.add(R.id.f1_content,fragment_collect);
                }
                else{
                    fragmentTransaction.show(fragment_collect);
                }
                iv_collect.setImageResource(R.drawable.collect1);
                break;
            case 2:
                if (fragment_search==null){
                    fragment_search=new SearchFragment();
                    fragmentTransaction.add(R.id.f1_content,fragment_search);
                }
                else{
                    fragmentTransaction.show(fragment_search);
                }
                iv_search.setImageResource(R.drawable.search1);
                break;
            case 3:
                if (fragment_user==null){
                    fragment_user=new UserFragment();
                    Bundle bundle=new Bundle();
                    bundle.putString("userid",userid);
                    fragment_user.setArguments(bundle);
                    fragmentTransaction.add(R.id.f1_content,fragment_user);
                }
                else{
                    fragmentTransaction.show(fragment_user);
                }
                iv_user.setImageResource(R.drawable.user1);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
    public void HideFragement(){
        if(fragment_index!=null){
            fragmentTransaction.hide(fragment_index);
        }
        if (fragment_collect!=null){
            fragmentTransaction.hide
                    (fragment_collect);
        }
        if (fragment_search!=null){
            fragmentTransaction.hide
                    (fragment_search);
        }
        if (fragment_user!=null){
            fragmentTransaction.hide
                    (fragment_user);
        }
    }
}



